package org.iwidget.model.javascript;

import org.iwidget.desktop.model.ElementSound;
import org.iwidget.desktop.model.WidgetElement;
import org.mozilla.javascript.*;

/**
 *
 * @author Muhammad Hakim A
 */

public class SoundElementJS extends ScriptableObject
{

    public String getClassName()
    {
        if(elementData != null)
            return elementData.getName();
        else
            return "Scroller";
    }

    public SoundElementJS()
    {
        elementData = new ElementSound();
        elementData.setName("Sound");
        setDefaults();
    }

    public SoundElementJS(WidgetElement widget)
    {
        initialize(widget, "Sound");
    }

    public SoundElementJS(WidgetElement widget, String name)
    {
        initialize(widget, name);
    }

    public void initialize(WidgetElement widget, String name)
    {
        elementData = new ElementSound(widget);
        elementData.setName("Sound");
        setDefaults();
    }

    public void setElementData(ElementSound d)
    {
        elementData = d;
    }

    private void setDefaults()
    {
        try
        {
            defineProperty("data", org.iwidget.model.javascript.SoundElementJS.class, 0);
            defineProperty("name", org.iwidget.model.javascript.SoundElementJS.class, 0);
            String functions[] = {
                "play", "stop", "loop"
            };
            defineFunctionProperties(functions, org.iwidget.model.javascript.SoundElementJS.class, 0);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public synchronized void jsConstructor()
    {
        if(elementData.widget == null)
        {
            Scriptable scope = ScriptableObject.getTopLevelScope(this);
            Object obj = scope.get("widget", scope);
            elementData.widget = (WidgetElement)Context.toType(obj, org.iwidget.desktop.model.WidgetElement.class);
        }
    }

    public void play()
    {
        elementData.play();
    }

    public void jsFunction_play()
    {
        elementData.play();
    }

    public void stop()
    {
        elementData.stop();
    }

    public void jsFunction_stop()
    {
        elementData.stop();
    }

    public void loop()
    {
        elementData.loop();
    }

    public void jsFunction_loop()
    {
        elementData.loop();
    }

    public String getName()
    {
        return elementData.getName();
    }

    public void setName(String s)
    {
        elementData.setName(s);
    }

    public String getData()
    {
        return elementData.getData();
    }

    public void setData(String s)
    {
        elementData.setData(s);
    }

    private static final long serialVersionUID = 0x3633353733343136L;
    ElementSound elementData;
}